// 主初始化
// 2007-4-18

// 初始化布局
InitLayout = function()
{
   var layout;
   layout = new Ext.BorderLayout(document.body, {
        north: {
            split:false,
            initialSize: 52,
            titlebar: false
	        },
        west: {
	        split:true,
	        initialSize: 170,
	        minSize: 50,
	        maxSize: 400,
	        titlebar: true,
	        collapsible: true,
	        animate: true,
	        useShim:true,
	        cmargins: {top:0,bottom:0,right:0,left:0}
        },
        south: {
            split:false,
            initialSize: 25,
            titlebar: false
        },
        center: {
            autoScroll:true,
            tabPosition: 'top',
            closeOnTab: true,
            alwaysShowTabs: true,
            resizeTabs: true,
			fitToFrame:true
        }
    });

	layout.beginUpdate();
	layout.add('north', new Ext.ContentPanel('MainMenubar'));
	layout.add('south', new Ext.ContentPanel('MainStatusbar'));
	layout.add('west', new Ext.ContentPanel('MainNavigation', {title: '导航栏', fitToFrame:true}));
	layout.add('center', new Ext.ContentPanel('MainPanel', {title:'主界面', fitToFrame:true}));

    //layout.restoreState();

	layout.showPanel('MainPanel');
	layout.endUpdate();

	return layout;
}


// 初始化主菜单
InitMainMenu = function(MenubarID)
{
	var aspicioMenuBar = new Ext.menu.Menubar({
	    orientation: "horizontal"
	});

	aspicioMenuBar.add(
	new Ext.menu.Item( {
		text: '个人空间',menu : {items: [
	        {id:"xiugaimima",text:"修改密码...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"xuodingxitong",text:"锁定系统...",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"shujubeifen",text:"数据备份...",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"Exit",text:"退出...",handler:function(){SetMainPanelUrl('a.htm')}}
	    ]}
	}),
	new Ext.menu.Item( {
		text: '房源管理',menu : {items: [
	        {id:"fangyuanguanli",text:"房源管理...",handler:function(){SetMainPanelUrl(BASE_URL + '/house/manager', '房源管理')}},
	        '-',
	        {id:"chengjiaojilu",text:"主要职能...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"guanggaoguanli",text:"广告管理...",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"fangyuanzhuanyi",text:"房源转移...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"chongpanchuli",text:"重盘处理...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"yongjinguanli",text:"佣金管理...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"fangyuanrenwu",text:"房源任务...",handler:function(){SetMainPanelUrl('a.htm')}}
		]}}),
	new Ext.menu.Item({
		text: '客源管理',menu : {items: [
	        {id:"keyuanguanli",text:"客源管理...",handler:function(){SetMainPanelUrl(BASE_URL + '/customer/manager', '客源管理')}},
	        {id:"keyuanzhuanyi",text:"客源转移...",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"genjinguanli",text:"跟进管理...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"keyuanrenwu",text:"客源任务...",handler:function(){SetMainPanelUrl('a.htm')}}
		]}}),
	new Ext.menu.Item( {
		text: '收租管理',menu : {items: [
		    {id:"shouzuguanli",text:"收租管理...",handler:function(){SetMainPanelUrl('a.htm')}}
		]}}),
	new Ext.menu.Item( {
		text: '统计报表',menu : {items: [
	        {id:"yejitongji",text:"业绩统计...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"zonghetongji",text:"综合统计...",handler:function(){SetMainPanelUrl('a.htm')}}
	    ]}}),
	new Ext.menu.Item( {
		text: '系统维护',menu : {items: [
	        {id:"bumenweihu",text:"部门维护...",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"loupanzidian",text:"楼盘字典...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"xitongcanshu",text:"系统参数...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"rizhiguanli",text:"日志管理...",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"huishouzhan",text:"回收站...",handler:function(){SetMainPanelUrl('a.htm')}}
	    ]}}),
	new Ext.menu.Item( {
		text: '常用工具',menu : {items: [
	        {id:"heimingdanguanli",text:"黑名单管理...",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"yuangongtongxunlu",text:"员工通讯录...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"duihuayujiaoliu",text:"对话与交流...",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"xinwengonggao",text:"新闻公告...",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"yewuwenda",text:"业务问答...",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"daikuanjisuanqi",text:"贷款计算器...",handler:function(){SetMainPanelUrl('a.htm')}}
	    ]}}),
	new Ext.menu.Item( {
		text: '合同文档',menu : {items: [
	        {id:"tiaojihetong",text:"调剂合同",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"fangwumaimaizhihuanhetong",text:"房屋买卖置换合同",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"fangwuzulinhetong",text:"房屋租赁合同",handler:function(){SetMainPanelUrl('a.htm')}},
	        '-',
	        {id:"diaoruhetong",text:"调入合同",handler:function(){SetMainPanelUrl('a.htm')}},
	        {id:"diaochuhetong",text:"调出合同",handler:function(){SetMainPanelUrl('a.htm')}}
	    ]}}),
	new Ext.menu.Item( {
		text: '帮助',menu : {items: [
	        {id:"about",text:"关于...",handler:function(){SetMainPanelUrl('a.htm')}}
	    ]}})
	);
//	aspicioMenuBar.render();
	aspicioMenuBar.show(Ext.get(MenubarID), "tl-tl");

	return aspicioMenuBar;
}


// 初始化工具栏
InitToolbar = function(ToolbarID)
{
    var menu1 = new Ext.menu.Menu({
        id: 'mainMenu',
        items: [
        	{text:'aaaaaaaaaaa'},
            '-',
            {text:'中文'}
        ]
    });

    var tb = new Ext.Toolbar(ToolbarID);

    tb.add({
            //cls: 'x-btn-text-icon bmenu', // icon and text class
            text:'工具栏按钮1',
            menu: menu1  // assign menu by instance
    });
    tb.add({
            text:'工具栏按钮2'
    });
    tb.add({
            text:'工具栏按钮3',
            handler: function(){}
    });
    tb.add({
            text:'刷新当前页面',
            handler: onReloadClick
    });

    return tb;
}


// 初始化主导航树
InitMainTree = function(TreeID)
{
    var Tree = Ext.tree;
    var tree = new Tree.TreePanel(TreeID, {
        animate:true,
        enableDD:false,
        containerScroll: true
    });

    // set the root node
    var root = new Tree.TreeNode({
        text: '随县政务公开管理系统'
    });
    tree.setRootNode(root);
    
    var child1 = new Tree.TreeNode({
        text: '资金管理'
    });
    var child2 = new Tree.TreeNode({
        text: '房源管理'
    });
    var child3 = new Tree.TreeNode({
        text: '客源管理'
    });
    var child4 = new Tree.TreeNode({
        text: '业务动态'
    });
    var child5 = new Tree.TreeNode({
        text: '实用工具'
    });
    var child6 = new Tree.TreeNode({
        text: '业务设置'
    });

	child1.appendChild(
		new Tree.TreeNode({text:'预算资金管理',url:BASE_URL + '/house/manager',qtip:'预算资金管理'}),
		new Tree.TreeNode({text:'专项资金管理',url:BASE_URL + '/customer/manager',qtip:'专项资金管理'}),
		new Tree.TreeNode({text:'专项资金类型',url:BASE_URL + '/test/manager',qtip:'专项资金类型'}),
		new Tree.TreeNode({text:'专项资金名称',url:'',qtip:'专项资金名称'}),
		new Tree.TreeNode({text:'通讯录',url:'',qtip:'通讯录'})
	);
	child2.appendChild(
		new Tree.TreeNode({text:'房源管理',url:BASE_URL + '/orgation/',qtip:'主要职能'}),
		new Tree.TreeNode({text:'主要职能',url:Bation/',qtip:'主要职能'}),
		new Tree.TreeNode({text:'广告管理',url:''}),
		new Tree.TreeNode({text:'成交记录',url:''}),
		new Tree.TreeNode({text:'收租管理',url:''}),
		new Tree.TreeNode({text:'房源任务',url:''})
	);
	child3.appendChild(
		new Tree.TreeNode({text:'客源管理',url:''}),
		new Tree.TreeNode({text:'跟进列表',url:''}),
		new Tree.TreeNode({text:'客源任务',url:''})
	);
	child4.appendChild(
		new Tree.TreeNode({text:'综合统计',url:''}),
		new Tree.TreeNode({text:'业绩统计',url:''}),
		new Tree.TreeNode({text:'新闻公告',url:''}),
		new Tree.TreeNode({text:'业务问答',url:''}),
		new Tree.TreeNode({text:'对话与交流',url:''}),
		new Tree.TreeNode({text:'通讯录',url:''})
	);
	child5.appendChild(
		new Tree.TreeNode({text:'贷款计算器',url:''}),
		new Tree.TreeNode({text:'黑名单管理',url:''}),
		new Tree.TreeNode({text:'回收站',url:''})
	);
	child6.appendChild(
		new Tree.TreeNode({text:'部门管理',url:''}),
		new Tree.TreeNode({text:'楼盘字典',url:''}),
		new Tree.TreeNode({text:'系统参数',url:''}),
		new Tree.TreeNode({text:'日志管理',url:''})
	);

    root.appendChild(child1,child2,child3,child4,child5,child6);

	tree.getSelectionModel().on('selectionchange',function(sm, node){
			if (node.attributes.url)
				SetMainPanelUrl(node.attributes.url, node.text);
		}
	);

    // render the tree
    tree.render();

    return tree;
}


/**
 * Simple tree node class based on Collapser and predetermined markup.
 */
var NavNode = function(clickEl, collapseEl){
    this.clickEl = Ext.get(clickEl);
    if(!collapseEl){
        collapseEl = this.clickEl.dom.nextSibling;
        while(collapseEl.nodeType != 1){
            collapseEl = collapseEl.nextSibling;
        }
    }
    this.collapseEl = Ext.get(collapseEl);
    this.clickEl.addClass('collapser-expanded');
    this.clickEl.mon('click', function(){
        this.collapsed === true ? 
            this.expand() : this.collapse();
    }, this, true);
}

NavNode.prototype = {
    collapse : function(){
        this.collapsed = true;
        this.collapseEl.setDisplayed(false);
        this.clickEl.replaceClass('collapser-expanded','collapser-collapsed');
    },
    
    expand : function(){
        this.collapseEl.setDisplayed(true);
        this.collapsed = false;
        this.collapseEl.setStyle('height', '');   
        this.clickEl.replaceClass('collapser-collapsed','collapser-expanded');
    }
}

function onReloadClick(btn){
    var item = LayoutObject.getRegion('center').getTabs().getActiveTab();
    var iframe = item.bodyEl.dom;
    var doc = (iframe.contentWindow || iframe.contentDocument);
    if (doc.document)
    {
        doc = doc.document;
    }
    doc.location.reload();
}
